global class AutoDeleteMedicalDashboard implements schedulable{

        global void execute(schedulablecontext sc){
            list<MedicalDashboard__c> deletemdb=new list<MedicalDashboard__c>();
            list<MedicalDashboard__c> mdbl=new list<MedicalDashboard__c>();
            set<id> idset=new set<id>();
            mdbl=[select id,createddate,month__c,year__c,ownerid from MedicalDashboard__c];
            map<string,medicaldashboard__c> mdblMap = new map<string,medicaldashboard__c>();
            for(medicaldashboard__c m:mdbl)
            {
            	mdblMap.put(m.month__c + m.year__c + m.ownerid, m);
            }
            string keyCom;
            for(medicaldashboard__c m:mdbl){
            	keyCom = m.month__c + m.year__c + m.ownerid;
            	if(mdblMap.containsKey(keyCom))
            	{
                    if(m.createddate==mdblMap.get(keyCom).createddate){
                        continue;
                    }
                    else if(m.createddate>mdblMap.get(keyCom).createddate){
                        idset.add(mdblMap.get(keyCom).id);
                    }else{
                        idset.add(m.id);
                    }            		
            	}
            	
            	
/*            	
                for(medicaldashboard__c m1:mdbl){
                if(m.month__c==m1.month__c && m.year__c==m1.year__c && m.ownerid==m1.ownerid){
                    if(m.createddate==m1.createddate){
                        continue;
                    }
                    else if(m.createddate>m1.createddate){
                        idset.add(m1.id);
                    }else{
                        idset.add(m.id);
                    }
                  }
                }
*/                                
            }
            
            deletemdb=[select id from medicaldashboard__c where id in:idset];
            delete deletemdb;
             
             //add by xiaojiang 100919
             list<TradeDashboardForPDA__c> lstDelDashPDA=[select id from TradeDashboardForPDA__c where CreatedDate<=:DateTime.valueOf((Date.today().addDays(-7) .format())+'  23:59:59')];
             if(lstDelDashPDA!=null&&lstDelDashPDA.size()>0)
             {
               delete lstDelDashPDA;
             }
             //end create
                
        }
}